package org.chen.system.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.email.dto.EmailConfigDTO;
import org.chen.common.email.request.EmailNotificationRequest;
import org.chen.common.export.dto.ExportConfigDTO;
import org.chen.common.flight.dto.FlightApiConfigDTO;
import org.chen.common.hotel.dto.HotelApiConfigDTO;
import org.chen.common.scene.dto.SceneApiConfigDTO;
import org.chen.common.system.dto.SystemStatusDTO;
import org.chen.common.result.ResultVO;
import org.chen.system.service.SystemConfigService;
import org.chen.system.service.impl.EmailNotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Email;

@Slf4j
@RestController
@Validated
@Tag(name = "不重要的接口", description = "不重要的接口")
public class SystemConfigController {
    
    @Autowired
    private SystemConfigService systemConfigService;

    @Autowired
    private EmailNotificationService emailNotificationService;

    @GetMapping("/api-config")
    public ResultVO<FlightApiConfigDTO> getApiConfig() {
        return ResultVO.success(systemConfigService.getFlightApiConfig());
    }

    @PostMapping("/api-config")
    public ResultVO<String> saveApiConfig(@Valid @RequestBody FlightApiConfigDTO apiConfig) {
        systemConfigService.saveApiConfig(apiConfig);
        return ResultVO.success("API配置保存成功");
    }

    @GetMapping("/hotel/api-config")
    public ResultVO<HotelApiConfigDTO> getHotelApiConfig() {
        return ResultVO.success(systemConfigService.getHotelApiConfig());
    }

    @PostMapping("/hotel/api-config")
    public ResultVO<String> saveHotelApiConfig(@Valid @RequestBody HotelApiConfigDTO apiConfig) {
        systemConfigService.saveHotelApiConfig(apiConfig);
        return ResultVO.success("API配置保存成功");
    }

    @GetMapping("/scene/api-config")
    public ResultVO<SceneApiConfigDTO> getSceneApiConfig() {
        SceneApiConfigDTO sceneApiConfig = systemConfigService.getSceneApiConfig();
        return ResultVO.success(sceneApiConfig);
    }

    @PostMapping("/scene/api-config")
    public ResultVO<String> saveSceneApiConfig(@Valid @RequestBody SceneApiConfigDTO apiConfig) {
        systemConfigService.saveSceneApiConfig(apiConfig);
        return ResultVO.success("API配置保存成功");
    }

    @GetMapping("/export-config")
    public ResultVO<ExportConfigDTO> getExportConfig() {
        return ResultVO.success(systemConfigService.getExportConfig());
    }

    @PostMapping("/export-config")
    public ResultVO<String> saveExportConfig(@Valid @RequestBody ExportConfigDTO exportConfig) {
        systemConfigService.saveExportConfig(exportConfig);
        return ResultVO.success("导出配置保存成功");
    }

    @GetMapping("/email-config")
    public ResultVO<EmailConfigDTO> getEmailConfig() {
        EmailConfigDTO emailConfig = systemConfigService.getEmailConfig();
        emailConfig.setPassword("前端不展示密码");
        return ResultVO.success(emailConfig);
    }

    @PostMapping("/send-email")
    public Boolean sendEmailNotification(@RequestBody EmailNotificationRequest request) {
        try {
            emailNotificationService.sendTaskExecutionNotification(request.getTask(), request.getResult());

            return Boolean.TRUE;
        } catch (Exception e) {
            log.error(e.getMessage());
            return Boolean.FALSE;
        }
    }

    @PostMapping("/email-config")
    public ResultVO<String> saveEmailConfig(@Valid @RequestBody EmailConfigDTO emailConfig) {
        systemConfigService.saveEmailConfig(emailConfig);
        return ResultVO.success("邮件配置保存成功");
    }

    @PostMapping("/test-email")
    public ResultVO<Boolean> testEmailSending(@RequestParam @Email String testEmail) {
        Boolean result = systemConfigService.testEmailSending(testEmail);
        return ResultVO.success(result ? "测试邮件发送成功" : "测试邮件发送失败", result);
    }

    @GetMapping("/status")
    public ResultVO<SystemStatusDTO> getSystemStatus() {
        return ResultVO.success(systemConfigService.getSystemStatus());
    }

    @PostMapping("/reset")
    public ResultVO<String> resetAllConfigs() {
        systemConfigService.resetAllConfigs();
        return ResultVO.success("配置已重置为默认值");
    }
}